-
Notifications
You must be signed in to change notification settings - Fork 8k
VAR|TMP overhaul #20628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
VAR|TMP overhaul #20628
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
2a3d9dd to
9ddff66
Compare
This comment was marked as outdated.
This comment was marked as outdated.
0e52da0 to
4ad5861
Compare
| if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY && MAY_BE_EMPTY_ONLY(t1)) { | ||
| return false; | ||
| } | ||
| return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
YIELD_FROM from generators can throw if the generator is closed, even for empty arrays. So this optimization may be unsound.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not mind doing the inverse - changing YIELD_FROM to not throw on empty array, though...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking into account the "closed" generator, this is wrong.
dstogov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should work, but the effect is not great.
(I see 30KB reduction in PHP code size and very slight performance difference).
The path breaks Symfony demo with function JIT (probably because of missing changes in FETCH_(DIM|OBJ)_FUNC_ARG handlers). This needs to be fixed of course.
| MAKE_NOP(opline); | ||
| ++(*opt_count); | ||
| if (src->op1_type & (IS_VAR|IS_TMP_VAR)) { | ||
| src->opcode = ZEND_FREE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replacing QM_ASSIGN with FREE opens possibility to deeper application of the same optimization.
| if ((t1 & (MAY_BE_ANY|MAY_BE_UNDEF)) == MAY_BE_ARRAY && MAY_BE_EMPTY_ONLY(t1)) { | ||
| return false; | ||
| } | ||
| return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Taking into account the "closed" generator, this is wrong.
|
I think the patch makes sense, but still has problems. |
Thank you, I missed those. I'll have a look. |
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
0f72e19 to
ca60df5
Compare
AWS x86_64 (c7i.24xl)
Laravel 12.11.0 demo app - 100 consecutive runs, 50 warmups, 100 requests (sec)
Symfony 2.8.0 demo app - 100 consecutive runs, 50 warmups, 100 requests (sec)
Wordpress 6.9 main page - 100 consecutive runs, 20 warmups, 20 requests (sec)
bench.php - 100 consecutive runs, 10 warmups, 2 requests (sec)
|
39de75b to
7fa98fd
Compare
0c56d0d to
838adce
Compare
838adce to
bdb57c6
Compare
The aim of this PR is twofold:
- Reduce the number of highly similar TMP|VAR handlers
- Avoid ZVAL_DEREF in most of these cases
This is achieved by guaranteeing that all zend_compile_expr() calls, as well as
all other compile calls with BP_VAR_{R,IS}, will result in a TMP variable. This
implies that the result will not contain an IS_INDIRECT or IS_REFERENCE value,
which was mostly already the case, with two exceptions:
- Calls to return-by-reference functions. Because return-by-reference functions
are quite rare, this is solved by delegating the DEREF to the RETURN_BY_REF
handler, which will examine the stack to check whether the caller expects a
VAR or TMP to understand whether the DEREF is needed. Internal functions will
also need to adjust by calling the zend_return_unwrap_ref() function.
- By-reference assignments, including both $a = &$b, as well as $a = [&$b]. When
the result of these expressions is used in a BP_VAR_R context, the reference
is unwrapped via a ZEND_QM_ASSIGN opcode beforehand. This is exceptionally
rare.
Closes phpGH-20628
399ef34 to
824a4ab
Compare
|
@iluuu1994 I'm afraid there are issues with this commit. In my CI both Symfony and Revive Adserver cannot run the test suite. Symfony 7.4 doesn't even start: In order to replicate just clone symfony 7.4, run composer install, then run ./phpunit with a freshly compiled php from master. In the Revive Adserver suite, I'm getting: In this case it might just be some deprecation being promoted to error perhaps. Code is very much legacy, so it might just be something that needs to be adjusted on our side. I will report back my findings on this. |
|
@mbeccati Thanks for letting me know. I'll have a look today. I did test this change with nightly, which runs all Symfony tests (along with many others). |
|
@iluuu1994 Thanks! I can confirm that the Revive Test Suite fails to run because of: vs: which I'm totally OK with, but will probably need to be listed in UPGRADING (or RFC?). Simple enough to replicate: |
|
@mbeccati Changing behavior was not the intention. I'll make sure this keeps working. The first case might just be a bad assertion, we'll see. |
|
@mbeccati The first issue should be solved. It was just a faulty assertion ("surely adding this code after testing is fine, right?"). The other will take more consideration. Sadly, PHP has inconsistent error types when passing non-vars as references: https://3v4l.org/lE72G Maybe it wouldn't be such a bad idea to unify this case after all, but that would need an RFC. I'll see how easy it is to find a resolution tonight, I'm out of time for now. |
The aim of this PR is twofold:
This is achieved by guaranteeing that all zend_compile_expr() calls, as well as all other compile calls with BP_VAR_R, will result in a TMP variable. This implies that the result will not contain an IS_INDIRECT or IS_REFERENCE value, which was mostly already the case, with two exceptions:
Calls to return-by-reference functions. Because return-by-reference functions are quite rare, this is solved by delegating the DEREF to the RETURN_BY_REF handler, which will examine the stack to check whether the caller expects a VAR or TMP to understand whether the DEREF is needed.
By-reference assignments, including both
$a = &$b, as well as[&$a] = $b. When the result of these expressions is used in a BP_VAR_R context, it will be passed to a new ZEND_DEREF opcode beforehand. This is exceptionally rare.Preliminary testing shows a 1.1% wall time improvement in Symfony Demo and roughly 0.5% in Wordpress. Edit: Sadly I can now only measure a 0.15% improvement for Symfony, but 0.8% for Wordpress. Zend/bench.php improves by ~3% in my tests. There seems to be quite a bit of volatility involved, potentially in relation to binary layout. Regardless, I think this is unlikely to cause true slowdowns for code that doesn't use return-by-ref.
TODOs:
zend_unwrap_reference(). I missed this function, I was looking for a macro.DEREFwithQM_ASSIGN.R/IScompile-paths, asserting noVARs are generated.